<?php

namespace App\Admin\Forms;

use App\Models\Order;
use App\Models\OrderService;
use App\Models\OrderServiceEmail;
use App\Models\OrderServicePolishReport;
use App\Models\OrderServiceProfile;
use App\Models\OrderServiceReport;
use App\Models\OrderServiceSubmitDetail;
use App\Models\OrderServiceSyntax;
use App\Models\OrderServiceSystemSign;
use App\Models\OrderVoidRecord;
use App\Models\User;
use App\Support\Data;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Form as DcatForm;


class VoidOrderForm extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        // 获取外部传递参数
        $order_id = $input['order_id'] ?? null;

        if (!$order_id) {
            return $this->response()->error('参数错误');
        }

        $data = [
            'order_id' => $input['order_id'],
            'contents' => $input['contents'],
            'admin_id' => $input['admin_id'],
        ];
        $result = OrderVoidRecord::create($data);

        if ($result) {
            Order::where('id', $order_id)->update([
                'order_name' => null,
                'order_status' => 0,
                'order_customer' => null,
                'order_amount' => null,
                'pay_status' => 0,
                'pay_amount' => null,
                'pay_type' => null,
                'pay_ticket' => null,
            ]);
            OrderService::where('order_id', $order_id)->update([
                'title' => null,
                'is_history' => null,
                'history' => null,
                'is_review' => 0,
                'is_syntax' => 0,
                'bibliometric_report' => null,
                'journal_report' => null,
                'cover_page' => null,
                'contents_page' => null,
                'full_report' => null,
                'is_polish' => 0,
                'is_target' => 0,
                'target_journal' => null,
                'author_info' => null,
            ]);
            OrderServiceEmail::where('order_id', $order_id)->delete();
            OrderServicePolishReport::where('order_id', $order_id)->delete();
            OrderServiceProfile::where('order_id', $order_id)->delete();
            OrderServiceReport::where('order_id', $order_id)->delete();
            OrderServiceSubmitDetail::where('order_id', $order_id)->delete();
            OrderServiceSyntax::where('order_id', $order_id)->delete();
            OrderServiceSystemSign::where('order_id', $order_id)->delete();
            return $this->response()->success('成功')->refresh();
        } else {
            return $this->response()->error('失败');
        }
    }

    public function form()
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;
        $this->hidden('order_id')->value($id);
        $this->hidden('admin_id')->value(Admin::user()->id);
        $this->textarea('contents', '放弃原因');
    }
}
